<?php

namespace app\admin\controller\goods;

use app\admin\library\Auth;
use app\common\controller\Backend;
use app\common\model\Storehouse;
use app\common\model\goods\GoodsHouseStock;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Stock extends Backend
{
    
    /**
     * Goods模型对象
     * @var \app\common\model\goods\Goods
     */
    protected $model = null;
    protected $searchFields = 'id,title,specs';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\goods\Goods;
        $this->view->assign("statusList", $this->model->getStatusList());
//        $Gmodel = new Gmodel;
//        $types= $Gmodel->goods_type();
//        $this->assignconfig('types',$types);
        $Storehouse = new Storehouse;
        $hlist = $Storehouse->select();
        $hslist = [];
        foreach ($hlist as $value){
            $hslist[$value['id']] = $value['name'];

        }
        $this->assignconfig('hslist',$hslist);




    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            $storehouse_id = 0;
            $filter = json_decode($this->request->get('filter'),true);
            $op = json_decode($this->request->get('op'),true);
            if(!empty($filter) && isset($filter['storehouse_id'])){
                $storehouse_id = $filter['storehouse_id'];
                unset($filter['storehouse_id']);
                unset($op['storehouse_id']);
            }
            $this->request->get(["filter"=> json_encode($filter),'op'=>json_encode($op)]);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                    ->with(['cate','supplier','brands'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['cate','supplier','brands'])
                    ->where($where)
//                    ->order($sort, $order)
                    ->order('weigh', $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','title','place_origin','number','unit','createtime','status','price','specs','stock','new_cost_price','weigh']);
                $row->visible(['cate']);
				$row->getRelation('cate')->visible(['name']);
				$row->visible(['supplier']);
				$row->getRelation('supplier')->visible(['nickname']);
                $row->visible(['brands']);
                $row->getRelation('brands')->visible(['name']);
            }

            $list = collection($list)->toArray();

            foreach ($list as $k=>  $v){

                if($storehouse_id>0){
                    $stock = new GoodsHouseStock;
                    $srow = $stock->where(array('goods_id'=>$v['id'],'storehouse_id'=>$storehouse_id))->find();
                    $list[$k]['stock'] = $srow['stock'];
                    $list[$k]['cumulative'] = $srow['cumulative'];
                    $list[$k]['weighting_cost'] = $srow['weighting_cost'];
                    $list[$k]['balance'] = $srow['balance'];
                    $list[$k]['new_cost'] = $srow['new_cost'];

                }else{
                    $stock = new GoodsHouseStock;
                    $list[$k]['stock'] = $stock->getstock($v['id'],session('admin')['storehouse_id']);
                }
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

}
